/*
** list_sort.c for liblist in /home/guina_b/
** 
** Made by benoit guina
** Login   <guina_b@epitech.net>
** 
** Started on  Fri Jan 01 00:00:00 2010 benoit guina
** Last update Fri Jan 01 00:00:00 2010 benoit guina
*/

#include "list.h"

static void	*list_sort_rec(void *l, int list_size,
			       int (*cmp)(void *, void *))
{
  void		*l2;

  l2 = 0;
  if (list_size > 1)
    {
      l2 = list_split(&l, list_size / 2);
      l = list_sort_rec(l, list_size / 2, cmp);
      l2 = list_sort_rec(l2, list_size / 2 + (list_size % 2), cmp);
      if (l2 && cmp(l, l2) > 0)
	return (list_sorted_merge(l2, l, cmp));
    }
  return (list_sorted_merge(l, l2, cmp));
}

void	*list_sort(void **l, int (*cmp)(void *, void *))
{
  *l = list_sort_rec(*l, list_size(*l), cmp);
  return (*l);
}
